package com.industrial.governance.service.impl;

import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.governance.domain.CityCount;
import com.industrial.governance.domain.QueryParams;
import com.industrial.governance.domain.SysArea;
import com.industrial.governance.mapper.BigPingServiceMapper;
import com.industrial.governance.service.IBigPingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BigPingServiceImpl implements IBigPingService {

    @Autowired
    private BigPingServiceMapper bigPingServiceMapper;

    /**
     * 各部门数据采集维度统计
     *
     * @return
     */
    @Override
    public List<Map<String,Object>> queryDataCaiJi() {
        Map<String,Object> r = bigPingServiceMapper.queryDataCaiJi();
        List<Map<String,Object>> list = new ArrayList<>();
        for(Map.Entry<String,Object> t : r.entrySet()){
            String key = t.getKey();
            Object value = t.getValue();
            Map<String,Object> tempMap = new HashMap<>(16);
            if(StringUtils.equals(key,"tongjifieldcount")){
                tempMap.put("name","统计局");
            }else if(StringUtils.equals(key,"guotufieldcount")){
                tempMap.put("name","自然资源和规划局");
            }else if(StringUtils.equals(key,"huanbaofieldcount")){
                tempMap.put("name","生态环境局");
            }else if(StringUtils.equals(key,"renshefieldcount")){
                tempMap.put("name","人力资源与保障局");
            }else if(StringUtils.equals(key,"shuilifieldcount")){
                tempMap.put("name","水利局");
            }else if(StringUtils.equals(key,"shuiwufieldcount")){
                tempMap.put("name","税务局");
            }
            tempMap.put("value",value);
            list.add(tempMap);
        }
        return list;
    }

    @Override
    public Map<String, Object> queryCompanyCount(QueryParams params) {
        Map<String,Object> result = new HashMap<>(16);
        //查询已采集部门的个数
        int importCount = 0;
        //查询统计局采集的部门
        List<String> tjCount = bigPingServiceMapper.selectTongJiDeptImportCount(params);
        List<String> gtCount = bigPingServiceMapper.selectGuoTuDeptImportCount(params);
        List<String> hbCount = bigPingServiceMapper.selectHuanBaoDeptImportCount(params);
        List<String> rsCount = bigPingServiceMapper.selectRenSheDeptImportCount(params);
        List<String> slCount = bigPingServiceMapper.selectShuiLiDeptImportCount(params);
        List<String> swCount = bigPingServiceMapper.selectShuiWuDeptImportCount(params);

        if(tjCount != null && !tjCount.isEmpty()){
            int tjSize = tjCount.size();
            importCount += tjSize;
        }

        if(gtCount != null && !gtCount.isEmpty()){
            int gtSize = gtCount.size();
            importCount += gtSize;
        }

        if(hbCount != null && !hbCount.isEmpty()){
            int hbSize = hbCount.size();
            importCount += hbSize;
        }

        if(rsCount != null && !rsCount.isEmpty()){
            int rsSize = rsCount.size();
            importCount += rsSize;
        }

        if(slCount != null && !slCount.isEmpty()){
            int slSize = slCount.size();
            importCount += slSize;
        }

        if(swCount != null && !swCount.isEmpty()){
            int swSize = swCount.size();
            importCount += swSize;
        }
        result.put("importDept",importCount);

        //查询参评企业数量
        int cpCompanyCount = bigPingServiceMapper.selectCpCompanyCount(params);
        result.put("cpCompanyCount",cpCompanyCount);


        //查询未导入的部门
        //查询所有的市级区县
        List<SysArea> sysAreasList = bigPingServiceMapper.selectAllCountyInfo();
        if(sysAreasList != null && !sysAreasList.isEmpty()){
            //过滤出所有区县
            sysAreasList = sysAreasList.stream().filter(k -> !StringUtils.endsWith(k.getAreaCode(),"00")).collect(Collectors.toList());
            int departmentsImportedCount = sysAreasList.size() * 6;
            departmentsImportedCount = departmentsImportedCount - importCount;
            result.put("unImportDept",departmentsImportedCount);
        }

        //查询数据采集总量
//        LocalDate localDate = LocalDate.now();
//        String years=String.valueOf(localDate.getYear() - 1);
        int importTotalDataCount = bigPingServiceMapper.selectImportDataTotalCount(params.getYears());
        result.put("importTotalDataCount",importTotalDataCount);

        //查询导入字段的数量
        Map<String, Object> fieldCountMap = bigPingServiceMapper.queryDataCaiJi();
        long fieldCount = 0;
        for(Map.Entry<String, Object> temp : fieldCountMap.entrySet()){
            Long value = (Long)temp.getValue();
            fieldCount += value;
        }
        result.put("fieldCount",fieldCount);

        //查询异常数据总量
        int dataErrorCount = bigPingServiceMapper.queryImportDataErrorCount(params.getYears());
        result.put("dataErrorCount",dataErrorCount);

        //查询数据质量数据
        List<CityCount> cityCounts = bigPingServiceMapper.queryCityDataQuality(params);

        Integer qualityCount = 0;
//        if(cityCounts != null && !cityCounts.isEmpty()){
//            qualityCount = cityCounts.stream().map(k -> StringUtils.isBlank(k.getCount()) ? 0 : Integer.valueOf(k.getCount())).reduce(Integer::sum).get();
//        }
        qualityCount = bigPingServiceMapper.queryQualityRule();
        result.put("qualityCount",qualityCount);
        return result;
    }

    /**
     * 各地市数据质量问题数量
     *
     * @param params
     * @return
     */
    @Override
    public Map<String,List<String>> queryCityDataQuality(QueryParams params) {
        List<CityCount> cityCounts = bigPingServiceMapper.queryCityDataQuality(params);
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();

        for(CityCount t : cityCounts){
            String areaName = t.getAreaName();
            String count = t.getCount();

            xAxis.add(areaName);
            yAxis.add(count);
        }

        Map<String,List<String>> result = new HashMap<>(16);
        result.put("xAxis",xAxis);
        result.put("yAxis",yAxis);
        return result;
    }
}
